package com.gmzg.gmzgnews.topic.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.topic.model.News;
import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

/**
 * Created by liuyi_s on 2016/11/1 11:40.
 * Email：liuyi_s@foxmail.com
 */

public class TopicQuestionAdapter extends RecyclerView.Adapter<TopicQuestionAdapter.ViewHolder> implements View.OnClickListener {

    private List<News> mNewsList;
    private Context mContext;
    private onChildClickListener mClickListener;
    private RecyclerView mRecyclerView;

    public TopicQuestionAdapter(Context context, List<News> newsList) {
        mContext = context;
        mNewsList = newsList;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.mRecyclerView = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater
                .from(mContext)
                .inflate(
                        R.layout.item_topic_question,
                        parent,
                        false
                );
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        News news = mNewsList.get(position);
        holder.mTitle.setText(news.getTitle());
        holder.mText.setText(news.getDescription());
        holder.mTextSource.setText("来源："+news.getFromName());
        Picasso.with(mContext)
                .load("http://tnfs.tngou.net/img" + news.getImgUrl())
                .into(holder.mCover);
    }

    @Override
    public int getItemCount() {
        return mNewsList.size();
    }

    //onClick on each item
    @Override
    public void onClick(View v) {
        if (mRecyclerView != null && mClickListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(v);
            mClickListener.onChildClickListener(mRecyclerView,v,position,mNewsList.get(position).getFromUrl());
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView mTitle;
        private final TextView mText;
        private final ImageView mCover;
        private final TextView mTextSource;
        public ViewHolder(View itemView) {
            super(itemView);
            mTitle = ((TextView) itemView.findViewById(R.id.item_topic_question_title));
            mCover = ((ImageView) itemView.findViewById(R.id.item_topic_question_img));
            mText = ((TextView) itemView.findViewById(R.id.item_topic_question_context));
            mTextSource = ((TextView) itemView.findViewById(R.id.item_topic_question_news_source));
        }
    }

    public void addAll(Collection<? extends News> collection) {
        mNewsList.addAll(collection);
        notifyDataSetChanged();
    }

    public interface onChildClickListener{
        void onChildClickListener(RecyclerView parent, View view, int position, String urlData);
    }

    public void onChildClickListener(onChildClickListener listener){
        mClickListener = listener;
    }
}

